//
//  GBWeiboKeywordViewController.m
//  PublicSentimentMonitorSystem
//
//  Created by 郭斌 on 15/11/8.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "GBWeiboKeywordViewController.h"
#import "UniversialTextfield.h"
#import "GBWeiboTableview.h"

@interface GBWeiboKeywordViewController ()<UITextFieldDelegate>
{
    UniversialTextfield *nicknameTextfield; //输入昵称的textfield
    UIButton *searchBtn; //查询按钮
    UITapGestureRecognizer *tapGestureClick; //点击手势
    UILabel *hintLabel; //提示信息的label
    UIView *view11;
    GBWeiboTableview *weiboTable; //微博关键词的tableview
    NSMutableArray *weiboArray; //接收数据
//    NSMutableArray *originDataArray; //原始数据
}
@end

@implementation GBWeiboKeywordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WQColor(247, 249, 250);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newData3:) name:@"NewArray3" object:nil];
    
    //初始化点击手势，以判断是否点击的是除了uitextfield之外的地方
    [self createTapGesture];
    
    nicknameTextfield = [[UniversialTextfield alloc] initWithFrame:CGRectMake(0, 20, screenW, 35)];
    nicknameTextfield.backgroundColor = [UIColor whiteColor];
    nicknameTextfield.placeholder = @"请输入微博关键词";
    nicknameTextfield.layer.borderColor = [UIColor lightGrayColor].CGColor;
    //键盘类型
    nicknameTextfield.keyboardType = UIKeyboardTypeDefault;
    //返回按钮类型
    nicknameTextfield.returnKeyType = UIReturnKeyDone;
    //改变输入框起始位置
    [self setTextFieldLeftPadding:nicknameTextfield forWidth:10];
    nicknameTextfield.delegate = self;
    [self.view addSubview:nicknameTextfield];
    
    searchBtn = [UIButton buttonWithType:0];
    //searchBtn.backgroundColor = KDColor;
    searchBtn.frame = CGRectMake(screenW - 40, 20, 40, 35);
    [searchBtn setImage:[UIImage imageNamed:@"默认添加"] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:searchBtn];
    
    view11 = [[UIView alloc] initWithFrame:CGRectMake(0, 55, screenW, 30)];
    
    hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, screenW, 30)];
    hintLabel.textColor = [UIColor lightGrayColor];
    hintLabel.font = [UIFont systemFontOfSize:12];
    hintLabel.text = @"请输入微博关键词";
    [view11 addSubview:hintLabel];
    [self.view addSubview:view11];
    
    //创建tablevew
    [self createTable];
    // Do any additional setup after loading the view.
}

#pragma mark - 删除按钮，接收新的数据(微博)
-(void)newData3:(NSNotification *)note
{
    NSDictionary *user = [note userInfo];
    NSMutableArray *dataArr = [user objectForKey:@"deleteArray"];
    
    weiboArray = [NSMutableArray arrayWithArray:dataArr];
}

#pragma mark - 昵称中的查询操作
-(void)searchAction
{
    [nicknameTextfield resignFirstResponder];
    if(nicknameTextfield.text.length != 0)
    {
        //检查数据是否重复
        for(int i = 0; i < weiboArray.count; i++)
        {
            if([nicknameTextfield.text isEqualToString:weiboArray[i]])
            {
                UIAlertView *alertz = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入内容不能重复" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertz show];
                return;
            }
        }
        
        //将数据添加到数据数组中
        [weiboArray insertObject:nicknameTextfield.text atIndex:0];
        
        if(_dataArray == nil)
        {
            weiboTable.frame = CGRectMake(0, 85, screenW, 200);
        }
        weiboTable.dataArray = [NSMutableArray arrayWithArray:weiboArray];
        [weiboTable reloadData];
        
    }
    else
    {
        UIAlertView *alertz = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入内容不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertz show];
    }
}

#pragma mark - 创建tablevew
-(void)createTable
{
    if(weiboTable == nil)
    {
        if(_dataArray == nil)
        {
            weiboTable = [[GBWeiboTableview alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        }
        else
        {
            weiboTable = [[GBWeiboTableview alloc] initWithFrame:CGRectMake(0, 90, screenW, 200)];
        }
        weiboArray = [NSMutableArray arrayWithArray:_dataArray];
        weiboTable.dataArray = [NSMutableArray arrayWithArray:_dataArray];
        [self.view addSubview:weiboTable];
    }
}

#pragma mark - 初始化点击手势，以判断是否点击的是除了uitextfield之外的地方
-(void)createTapGesture
{
    //开启交互
    self.view.userInteractionEnabled = YES;
    tapGestureClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapClick)];
    [self.view addGestureRecognizer:tapGestureClick];
}

#pragma mark - 点击除了uitextfield之外的地方，键盘消失
-(void)TapClick
{
    if(nicknameTextfield.text.length == 0)
    {
        nicknameTextfield.placeholder = @"请输入微博关键词";
    }
    [nicknameTextfield resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [nicknameTextfield resignFirstResponder];
    return YES;
}

-(void)setTextFieldLeftPadding:(UITextField *)textField forWidth:(CGFloat)leftWidth
{
    CGRect frame = textField.frame;
    frame.size.width = leftWidth;
    UIView *leftview = [[UIView alloc] initWithFrame:frame];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftview;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [self.delegateWeiboKeyword weiboReturnTheKeyword:weiboArray];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
